﻿//显示原料
layui.use(['table', 'dropdown', 'layer', 'form'], function () {

    var table = layui.table;
    var dropdown = layui.dropdown;
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    $(function () {
        Show();
     });
    // 点击导出按钮
    document.getElementById('exportBtn').addEventListener('click', function () {
        var checkStatus = table.checkStatus('carriertable');  // 获取当前表格的选中状态
        var selectedData = checkStatus.data;  // 获取选中的数据
        if (selectedData.length === 0) {
            alert('请先选择要导出的数据!');
            return;
        }

        // 使用 XLSX 导出选中的数据
        var wb = XLSX.utils.book_new();
        var ws = XLSX.utils.json_to_sheet(selectedData); // 将选中的数据转换为工作表
        XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

        // 导出为 Excel 文件
        XLSX.writeFile(wb, '原料管理.xlsx');
    });
    window.Show = function Show() {
        var Ingredientnumber = $("#Ingredientnumber").val();
        var IngredientName = $("#IngredientName").val();
        var State = $("#State").val();
        console.log(Ingredientnumber)
        layui.use(function () {

            // 先进行表格的初始渲染
            table.render({
                elem: '#carriertable',  // 表格元素的选择器
                page: {
                    limit: 5,
                    limits: [5, 8, 10]
                },
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'ingredientnumber', title: '原料编号' },
                    { field: 'ingredientName', title: '原料名称' },
                    { field: 'ingredientFen', title: '原料分类' },
                    { field: 'specification', title: '规格型号' },
                    { field: 'size', title: '尺寸' },
                    { field: 'unit', title: '单位' },
                    { field: 'rawmaterialproperties', title: '原料属性' },
                    { field: 'num', title: '库存数量' },
                    { field: 'ingredientType', title: '原料类型' },
                    { field: 'state', title: '状态' },
                    { field: 'createdTimess', title: '创建时间' },
                    {
                        field: '', width: 260, title: '操作', templet: function (d) {
                            if (d.state == '禁用') {
                                return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="zhuang(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>启用</a>' +
                                    '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="FanUpdate(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a>' +
                                    '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="Look(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a>'+
                                    '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="IsDel(' + d.ingredientId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                            } else if (d.state == '启用') {
                                return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="zhuang(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>禁用</a>' +
                                    '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="FanUpdate(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a>' +
                                    '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="Look(' + d.ingredientId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a>' +
                                    '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="IsDel(' + d.ingredientId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                            }
     }
      }
                ]],
                // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                data: []
            });

            // 创建表格实例
            $.ajax({
                url: "../Ingredient/IngredientShow",
                type: "get",
                data: {
                    Pageindex: 1,
                    Pagesize: 100,
                    token: "",
                    Ingredientnumber: Ingredientnumber,
                    IngredientName: IngredientName,
                    State: State
                },

                success: function (data) {
                    data = JSON.parse(data);
                    console.log(data.data.list);
                    // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                    table.reload('carriertable', {
                        page: {
                            limit: 10,
                            limits: [5, 8, 10]
                        },
                        data: data.data.list
                    });
                },
                error: function () {
                    console.log("请求失败");
                }
            });
        });
    }
    //删除
    window.IsDel = function IsDel(ingredientId) {
        console.log(ingredientId)
        var token = localStorage.getItem("token")
        var model = JSON.stringify({
            IngredientId: [ingredientId],
            DeletedBy: "",
            DeletedDate: ""
        })
        console.log(model)
        layer.confirm("确认删除吗", function () {
            $.ajax({
                url: '../Ingredient/IsDelDo',
                data: { model: model, token: token },
                type: 'post',
                success: function (res) {
                    console.log(res);
                    var res = JSON.parse(res);
                    if (res.data > 0) {
                        layer.msg("删除成功", { icon: 1 });
                        Show();
                    } else {
                        layer.msg("删除失败", { icon: 2 });
                    }
                }
            });
        });
    }
    //编辑
    window.FanUpdate = function FanUpdate(ingredientId) {
        localStorage.setItem("ingredientId", ingredientId)
        location.href = '/Ingredient/UpdateIngredient'
    }
    //查看
    window.Look = function Look(ingredientId) {
        localStorage.setItem("ingredientId", ingredientId)
        location.href = '/Ingredient/LookIngredient'
    }
      //修改状态
    window.zhuang = function zhuang(ingredientId) {
        console.log(ingredientId)
        $.ajax({
            url: '../Ingredient/IngredientFanDo',
            data: {
                IngredientId: ingredientId,
                token: ""
            },
            type: 'Get',
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res.data.state)
                if (res.data.state == "启用") {
                    $.ajax({
                        url: '../Ingredient/IsUpdateIngredientSarte',
                        data: {
                            IngredientId: ingredientId,
                            State: "禁用",
                            token: ""
                        },
                        type: 'post',
                        success: function (res) {
                            console.log(res);
                            var res = JSON.parse(res);
                            if (res.code ==1) {
                                layer.msg("成功", { icon: 1 });
                                Show();
                            } else {
                                layer.msg("失败", { icon: 2 });
                            }
                        }
                    });
                }
                else {
                    $.ajax({
                        url: '../Ingredient/IsUpdateIngredientSarte',
                        data: {
                            IngredientId: ingredientId,
                            State: "启用",
                            token: ""
                        },
                        type: 'post',
                        success: function (res) {
                            console.log(res);
                            var res = JSON.parse(res);
                            if (res.code ==1) {
                                layer.msg("成功", { icon: 1 });
                                Show();
                            } else {
                                layer.msg("失败", { icon: 2 });
                            }
                        }
                    });  
                }
            }
        })
     
    }

});